package com.zhiyou.aifeng.smart_machine

import android.os.Bundle
import android.widget.ImageView
import com.zhiyou.aifeng.common.base.gone
import com.zhiyou.aifeng.common.base.visible
import com.zhiyou.aifeng.common.helper.get
import com.zhiyou.aifeng.common_ui.base.*
import com.zhiyou.aifeng.common_ui.model.CommonData
import com.zhiyou.aifeng.common_ui.utils.bindLifeCycle
import com.zhiyou.aifeng.common_ui.utils.subscribeByFinally
import com.zhiyou.aifeng.utils.setEmptyIcon
import com.zhiyou.aifeng.viewmodel.RepairViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.startActivity
import kotlin.collections.count

class RepairActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_repair)
        initTitle(title = "农机维修站", divider = false)

        swipe_refresh.isRefreshing = true
        getData(pageNum)
    }

    override fun getViewModel() = get<RepairViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        initList()
    }

    private fun initList() {
        empty_img.setEmptyIcon()
        empty_hint.text = "暂无相关维修站信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.apply {
            loadLinear(baseContext, swipe_refresh) {
                if (!isLoadingMore) {
                    isLoadingMore = true
                    getData(pageNum)
                }
            }

            mAdapter = SlimAdapter.create()
                .register<CommonData>(R.layout.item_repair_list) { data, injector ->
                    injector.text(R.id.item_repair_name, data.name)
                        .text(R.id.item_repair_tel, "电话：${data.phone}")
                        .with<ImageView>(R.id.item_repair_img) {
                            val imgs = data.img.split(",")
                            it.loadImage(imgs[0])
                        }
                        .clicked(R.id.item_repair) {
                            startActivity<RepairDetailActivity>("id" to data.id)
                        }
                }
                .attachTo(this)
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getRepairList(index)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

}